import './admin_shop_show.html';

import { Template } from 'meteor/templating';
import { Shops } from '/imports/api/shops/shops.js';

import { Images } from '/imports/api/images/images.js';

Template.admin_shop_show.onCreated(function(){
  let self = this;

  self.shop = new ReactiveVar({
    name: "载入中",
    cover_url: "载入中"
  });
  self.autorun(function(){
    if (Meteor.userId != null) {
      let shop_id  = FlowRouter.getParam('id');
      if (shop_id) {
        let shop = Shops.findOne({_id: shop_id});
        // console.log(shop);
        self.shop.set(shop);
      }else{
          FlowRouter.go('/admin');
      }
    }
    else{
      FlowRouter.go('/login');
      Message.sendError("请先登录", '');
    }
  });
});

Template.admin_shop_show.helpers({
  shop_name(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().name;
  },
  address(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().address;
  },
  cover_url(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return '<img src="'+Template.instance().shop.get().cover_url+'">'
  },
  createdAt(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    let date = Template.instance().shop.get().createdAt;
    date = moment(date).format('YYYY年MMMMDD日, HH:mm:ss');
    return date;
  },
  customqq(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().customqq;
  },
  customwexin(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().customwexin;
  },
  description(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().description;
  },
  foundedAt(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().foundedAt;
  },
  hotline(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().hotLine;
  },
  hremail(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
      return Template.instance().shop.get().hremail;
  },
  hrqq(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().hrqq;
  },
  mail(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().mail;
  },
  province(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().province;
  },
  shopCharge(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().shopCharge;
  },
  shopCharge_id(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
  },
  tel(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
    return Template.instance().shop.get().tel;
  },
  area(){
    if (Template.instance().shop.get() === undefined) {
      return "载入中";
    }
      return Template.instance().shop.get().area;
  }

});

Template.admin_shop_show.events({
  "change .myFileInput"(event, instance){
    FS.Utility.eachFile(event, function(file) {
        Images.insert(file, function (err, fileObj) {
          if (err){
             console.log(err);
          } else {
             // handle success depending what you need to do
             console.log(instance.shop.get()._id);
             let interval = Meteor.setInterval( function() {
                if (fileObj.hasStored('images')) {
                  Meteor.call('shops.update.cover', instance.shop.get()._id, fileObj._id, function(error, result){
                    console.log(error);
                    console.log(result);
                    Meteor.clearInterval(interval);

                  });
                }
             });




          }
        });
     });
  }
});
